package controller;

/**
 * 
 * @author Yanren
 */

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import entity.DecisionLinesEvent;

import server.ClientData;
import server.ClientState;
import server.IProtocolHandler;
import server.Server;
import server.ServerThread;
import xml.Message;

public class AddChoiceController implements IProtocolHandler {

	@Override
	public Message process(ClientState state, Message request) {
		entity.System system = entity.System.getInstance();
		
		Node addChoiceRequest = request.contents.getFirstChild();
		NamedNodeMap map = addChoiceRequest.getAttributes();
		String eventId = map.getNamedItem("id").getNodeValue();
		String choice  = map.getNamedItem("choice").getNodeValue();
		int number     = Integer.valueOf(map.getNamedItem("number").getNodeValue());
		
		DecisionLinesEvent event = system.getEvent(eventId);
		event.setChoice(number,choice);
		
		system.updateEvent(eventId,event);
		
		NamedNodeMap requestMap = request.contents.getAttributes();
		String response = Message.responseHeader(requestMap.getNamedItem("id").getNodeValue());
		response += "<addChoiceResponse id='"+eventId+"' number='"+number+"' choice='"+choice+"' /></response>";
		
		
		
		// If we have all the choices, start the event
		if(event.getNumChoices() == event.getLines().size()){
			// Set phase (doesn't really matter)
			event.setPhase("adding edges");
			// Broadcast the add choice response to everyone (including this client)
			ServerThread.broadcastMessage(new Message(response), eventId, null);
			// Generate the turn response
			response = Message.responseHeader(requestMap.getNamedItem("id").getNodeValue())
					+ "<turnResponse completed='false'/></response>";
			// If its async, broadcast it
			if(event.getTurnType().equals("asynchronous")){
				ServerThread.broadcastMessage(new Message(response), eventId, state.id());
			}
			
			event.setTurn(((ClientData)state.getData()).getUsername());
			system.updateEvent(eventId, event);
			
			// Regardless of asynch or RR, send turn response back to this client
			// He's excluded on the broadcast above, so send it back to him if its async
			// Or if its RR, just make him go first
			return new Message(response);
		}else{
			// Not the last edge, broadcast the add choice response as normal
			ServerThread.broadcastMessage(new Message(response), eventId, state.id());
			return new Message(response);
		}
	
		
	}

}


/* Relevant schema
<!-- Update Choice To Event   -->
<!-- ________________________ -->

<xs:element name='addChoiceRequest'> 
  <xs:complexType>
    <xs:attribute name='id'         type='xs:string' use='required'/>
    <xs:attribute name='number'     type='xs:int' use='required'/>
    <xs:attribute name='choice'     type='xs:string' use='required'/>
  </xs:complexType>
</xs:element>

<xs:element name='addChoiceResponse'>
  <xs:complexType>
    <xs:attribute name='id'         type='xs:string' use='required'/>
    <xs:attribute name='number'     type='xs:int' use='required'/>
    <xs:attribute name='choice'     type='xs:string' use='required'/>
  </xs:complexType>
</xs:element> 
*/